<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>10.4. Zend_Db_Select</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="prev" href="zend.db.profiler.html" title="10.3. Zend_Db_Profiler">
<link rel="next" href="zend.db.table.html" title="10.5. Zend_Db_Table">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.db.select.html#zend.db.select.introduction" title="10.4.1. Overview of the Select Object">
<link rel="subsection" href="zend.db.select.html#zend.db.select.creating" title="10.4.2. Creating a Select Object">
<link rel="subsection" href="zend.db.select.html#zend.db.select.building" title="10.4.3. Building Select queries">
<link rel="subsection" href="zend.db.select.html#zend.db.select.execute" title="10.4.4. Executing Select Queries">
<link rel="subsection" href="zend.db.select.html#zend.db.select.other" title="10.4.5. Other methods">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">10.4. Zend_Db_Select</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.db.profiler.html">Prev</a> </td>
<th width="60%" align="center">Chapter 10. Zend_Db</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.db.table.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.db.select"></a>10.4. Zend_Db_Select</h2></div></div></div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.db.select.introduction"></a>10.4.1. Overview of the Select Object</h3></div></div></div>
<p>
            The Zend_Db_Select object represents a SQL <code class="code">SELECT</code> query statement. The
            class has methods for adding individual parts to the query. You can specify some
            parts of the query using PHP methods and data structures, and the class forms the
            correct SQL syntax for you. After you build a query, you can execute the query as if
            you had written it as a string.
        </p>
<p>
            The value offered by Zend_Db_Select includes:
        </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                    Object-oriented methods for specifying SQL queries in a piece-by-piece manner;
                </p></li>
<li><p>
                    Database-independent abstraction of some parts of the SQL query;
                </p></li>
<li><p>
                    Automatic quoting of metadata identifiers in most cases, to support identifiers
                    containing SQL reserved words and special characters;
                </p></li>
<li><p>
                    Quoting identifiers and values, to help reduce risk of SQL injection attacks.
                </p></li>
</ul></div>
<p>
            Using Zend_Db_Select is not mandatory. For very simple SELECT queries, it is usually
            simpler to specify the entire SQL query as a string and execute it using Adapter
            methods like <code class="code">query()</code> or <code class="code">fetchAll()</code>. Using Zend_Db_Select is
            helpful if you need to assemble a SELECT query procedurally, or based on conditional
            logic in your application.
        </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.db.select.creating"></a>10.4.2. Creating a Select Object</h3></div></div></div>
<p>
            You can create an instance of a Zend_Db_Select object using the <code class="code">select()</code>
            method of a Zend_Db_Adapter_Abstract object.
        </p>
<div class="example">
<a name="zend.db.select.creating.example-db"></a><p class="title"><b>Example 10.41. Example of the database adapter's select() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$db = Zend_Db::factory( ...options... );
$select = $db-&gt;select();
            </pre></div>
</div>
<br class="example-break"><p>
            Another way to create a Zend_Db_Select object is with its constructor, specifying the
            database adapter as an argument.
        </p>
<div class="example">
<a name="zend.db.select.creating.example-new"></a><p class="title"><b>Example 10.42. Example of creating a new Select object</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($db);
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.db.select.building"></a>10.4.3. Building Select queries</h3></div></div></div>
<p>
            When building the query, you can add clauses of the query one by one. There is a
            separate method to add each clause to the Zend_Db_Select object.
        </p>
<div class="example">
<a name="zend.db.select.building.example"></a><p class="title"><b>Example 10.43. Example of the using methods to add clauses</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Create the Zend_Db_Select object
$select = $db-&gt;select();

// Add a FROM clause
$select-&gt;from( ...specify table and columns... )

// Add a WHERE clause
$select-&gt;where( ...specify search criteria... )

// Add an ORDER BY clause
$select-&gt;order( ...specify sorting criteria... );
            </pre></div>
</div>
<br class="example-break"><p>
            You also can use most methods of the Zend_Db_Select object with a convenient fluent
            interface. A fluent interface means that each method returns a reference to the object
            on which it was called, so you can immediately call another method.
        </p>
<div class="example">
<a name="zend.db.select.building.example-fluent"></a><p class="title"><b>Example 10.44. Example of the using the fluent interface</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$select = $db-&gt;select()
    -&gt;from( ...specify table and columns... )
    -&gt;where( ...specify search criteria... )
    -&gt;order( ...specify sorting criteria... );
            </pre></div>
</div>
<br class="example-break"><p>
            The examples in this section show usage of the fluent interface, but you can use the
            non-fluent interface in all cases. It is often necessary to use the non-fluent
            interface, for example, if your application needs to perform some logic before adding a
            clause to a query.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.from"></a>10.4.3.1. Adding a FROM clause</h4></div></div></div>
<p>
                Specify the table for this query using the <code class="code">from()</code> method. You can
                specify the table name as a simple string. Zend_Db_Select applies identifier
                quoting around the table name, so you can use special characters.
            </p>
<div class="example">
<a name="zend.db.select.building.from.example"></a><p class="title"><b>Example 10.45. Example of the from() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT *
//   FROM "products"

$select = $db-&gt;select()
    -&gt;from( 'products' );
                </pre></div>
</div>
<br class="example-break"><p>
                You can also specify the correlation name (sometimes called the "table alias") for
                a table. Instead of a simple string, use an associative array mapping the
                correlation name to the table name. In other clauses of the SQL query, use this
                correlation name. If your query joins more than one table, Zend_Db_Select generates
                unique correlation names based on the table names, for any tables for which you
                don't specify the correlation name.
            </p>
<div class="example">
<a name="zend.db.select.building.from.example-cname"></a><p class="title"><b>Example 10.46. Example of specifying a table correlation name</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p.*
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from( array('p' =&gt; 'products') );
                </pre></div>
</div>
<br class="example-break"><p>
                Some RDBMS brands support a leading schema specifier for a table. You can specify
                the table name as "<code class="code">schemaName.tableName</code>", where Zend_Db_Select quotes
                each part individually, or you may specify the schema name separately. A schema
                name specified in the table name takes precedence over a schema provided separately
                in the event that both are provided.
            </p>
<div class="example">
<a name="zend.db.select.building.from.example-schema"></a><p class="title"><b>Example 10.47. Example of specifying a schema name</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT *
//   FROM "myschema"."products"

$select = $db-&gt;select()
    -&gt;from( 'myschema.products' );

// or

$select = $db-&gt;select()
    -&gt;from('products', '*', 'myschema');
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.columns"></a>10.4.3.2. Adding Columns</h4></div></div></div>
<p>
                In the second argument of the <code class="code">from()</code> method, you can specify the
                columns to select from the respective table. If you specify no columns, the default
                is "<code class="code">*</code>", the SQL wildcard for "all columns".
            </p>
<p>
                You can list the columns in a simple array of strings, or as an associative mapping
                of column alias to column name. If you only have one column to query, and you don't
                need to specify a column alias, you can list it as a plain string instead of an
                array.
            </p>
<p>
                If you give an empty array as the columns argument, no columns from the respective
                table are included in the result set. See a
                <a href="zend.db.select.html#zend.db.select.building.join.example-no-columns" title="Example 10.53. Example of specifying no columns">code example</a>
                under the section on the <code class="code">join()</code> method.
            </p>
<p>
                You can specify the column name as "<code class="code">correlationName.columnName</code>".
                Zend_Db_Select quotes each part individually. If you don't specify a correlation
                name for a column, it uses the correlation name for the table named in the current
                <code class="code">from()</code> method.
            </p>
<div class="example">
<a name="zend.db.select.building.columns.example"></a><p class="title"><b>Example 10.48. Examples of specifying columns</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'product_name'));

// Build the same query, specifying correlation names:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('p.product_id', 'p.product_name'));

// Build this query with an alias for one column:
//   SELECT p."product_id" AS prodno, p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('prodno' =&gt; 'product_id', 'product_name'));
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.columns-expr"></a>10.4.3.3. Adding Expression Columns</h4></div></div></div>
<p>
                Columns in SQL queries are sometimes expressions, not simply column names from a
                table. Expressions should not have correlation names or quoting applied. If your
                column string contains parentheses, Zend_Db_Select recognizes it as an expression.
            </p>
<p>
                You also can create an object of type Zend_Db_Expr explicitly, to prevent a string
                from being treated as a column name. Zend_Db_Expr is a minimal class that contains
                a single string. Zend_Db_Select recognizes objects of type Zend_Db_Expr and
                converts them back to string, but does not apply any alterations, such as quoting
                or correlation names.
            </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    Using Zend_Db_Expr for column names is not necessary if your column expression
                    contains parentheses; Zend_Db_Select recognizes parentheses and treats the
                    string as an expression, skipping quoting and correlation names.
                </p></td></tr>
</table></div>
<div class="example">
<a name="zend.db.select.building.columns-expr.example"></a><p class="title"><b>Example 10.49. Examples of specifying columns containing expressions</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", LOWER(product_name)
//   FROM "products" AS p
// An expression with parentheses implicitly becomes
// a Zend_Db_Expr.

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'LOWER(product_name)'));

// Build this query:
//   SELECT p."product_id", (p.cost * 1.08) AS cost_plus_tax
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'cost_plus_tax' =&gt; '(p.cost * 1.08)'));

// Build this query using Zend_Db_Expr explicitly:
//   SELECT p."product_id", p.cost * 1.08 AS cost_plus_tax
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'cost_plus_tax' =&gt; new Zend_Db_Expr('p.cost * 1.08')));
                </pre></div>
</div>
<br class="example-break"><p>
                In the cases above, Zend_Db_Select does not alter the string to apply correlation
                names or identifier quoting. If those changes are necessary to resolve ambiguity,
                you must make the changes manually in the string.
            </p>
<p>
                If your column names are SQL keywords or contain special characters, you should use
                the Adapter's <code class="code">quoteIdentifier()</code> method and interpolate the result into
                the string. The <code class="code">quoteIdentifier()</code> method uses SQL quoting to delimit
                the identifier, which makes it clear that it is an identifier for a table or a
                column, and not any other part of SQL syntax.
            </p>
<p>
                Your code is more database-independent if you use the
                <code class="code">quoteIdentifier()</code> method instead of typing quotes literally in your
                string, because some RDBMS brands use nonstandard symbols for quoting identifiers.
                The <code class="code">quoteIdentifier()</code> method is designed to use the appropriate
                quoting symbols based on the adapter type. The <code class="code">quoteIdentifier()</code>
                method also escapes any quote characters that appear within the identifier name
                itself.
            </p>
<div class="example">
<a name="zend.db.select.building.columns-quoteid.example"></a><p class="title"><b>Example 10.50. Examples of quoting columns in an expression</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query, quoting a special column name "from" in the expression:
//   SELECT p."from" + 10 AS origin
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('origin' =&gt; '(p.' . $db-&gt;quoteIdentifier('from') . ' + 10)'));
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.columns-atomic"></a>10.4.3.4. Adding columns to an existing FROM or JOIN table</h4></div></div></div>
<p>
                There may be cases where you wish to add columns to an existing FROM or JOIN table
                after those methods have been called. The <code class="code">columns()</code> method allows you
                to add specific columns at any point before the query is executed. You can supply
                the columns as either a string or <code class="code">Zend_Db_Expr</code> or as an array of these
                elements. The second argument to this method can be omitted, implying that the
                columns are to be added to the FROM table, otherwise an existing correlation name
                must be used.
            </p>
<div class="example">
<a name="zend.db.select.building.columns-atomic.example"></a><p class="title"><b>Example 10.51. Examples of adding columns with the <code class="code">columns()</code> method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'), 'product_id')
    -&gt;columns('product_name');

// Build the same query, specifying correlation names:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'), 'p.product_id')
    -&gt;columns('product_name', 'p'); // Alternatively use columns('p.product_name')
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.join"></a>10.4.3.5. Adding Another Table to the Query with JOIN</h4></div></div></div>
<p>
                Many useful queries involve using a <code class="code">JOIN</code> to combine rows from multiple
                tables. You can add tables to a Zend_Db_Select query using the <code class="code">join()</code>
                method. Using this method is similar to the <code class="code">from()</code> method, except you
                can also specify a join condition in most cases.
            </p>
<div class="example">
<a name="zend.db.select.building.join.example"></a><p class="title"><b>Example 10.52. Example of the join() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name", l.*
//   FROM "products" AS p JOIN "line_items" AS l
//     ON p.product_id = l.product_id

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'product_name'))
    -&gt;join(array('l' =&gt; 'line_items'),
        'p.product_id = l.product_id');
                </pre></div>
</div>
<br class="example-break"><p>
                The second argument to <code class="code">join()</code> is a string that is the join condition.
                This is an expression that declares the criteria by which rows in one table match
                rows in the the other table. You can use correlation names in this expression.
            </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    No quoting is applied to the expression you specify for the join condition; if
                    you have column names that need to be quoted, you must use
                    <code class="code">quoteIdentifier()</code> as you form the string for the join condition.
                </p></td></tr>
</table></div>
<p>
                The third argument to <code class="code">join()</code> is an array of column names, like that
                used in the <code class="code">from()</code> method. It defaults to "<code class="code">*</code>", supports
                correlation names, expressions, and Zend_Db_Expr in the same way as the array of
                column names in the <code class="code">from()</code> method.
            </p>
<p>
                To select no columns from a table, use an empty array for the list of columns. This
                usage works in the <code class="code">from()</code> method too, but typically you want some
                columns from the primary table in your queries, whereas you might want no columns
                from a joined table.
            </p>
<div class="example">
<a name="zend.db.select.building.join.example-no-columns"></a><p class="title"><b>Example 10.53. Example of specifying no columns</b></p>
<div class="example-contents">
<pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p JOIN "line_items" AS l
//     ON p.product_id = l.product_id

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id', 'product_name'))
    -&gt;join(array('l' =&gt; 'line_items'),
        'p.product_id = l.product_id',
        array() ); // empty list of columns
                </pre>
<p>
                    Note the empty <code class="code">array()</code> in the above example in place of a list of
                    columns from the joined table.
                </p>
</div>
</div>
<br class="example-break"><p>
                SQL has several types of joins. See the list below for the methods to support
                different join types in Zend_Db_Select.
            </p>
<div class="itemizedlist"><ul type="disc">
<li>
<p>
                        <span><strong class="command">INNER JOIN</strong></span> with the
                        <code class="code">join(table, join, [columns])</code> or
                        <code class="code">joinInner(table, join, [columns])</code> methods.
                    </p>
<p>
                        This may be the most common type of join. Rows from each table are compared
                        using the join condition you specify. The result set includes only the rows
                        that satisfy the join condition. The result set can be empty if no rows
                        satisfy this condition.
                    </p>
<p>
                        All RDBMS brands support this join type.
                    </p>
</li>
<li>
<p>
                        <span><strong class="command">LEFT JOIN</strong></span> with the
                        <code class="code">joinLeft(table, condition, [columns])</code> method.
                    </p>
<p>
                        All rows from the left operand table are included, matching rows from the
                        right operand table included, and the columns from the right operand table
                        are filled with NULLs if no row exists matching the left table.
                    </p>
<p>
                        All RDBMS brands support this join type.
                    </p>
</li>
<li>
<p>
                        <span><strong class="command">RIGHT JOIN</strong></span> with the
                        <code class="code">joinRight(table, condition, [columns])</code> method.
                    </p>
<p>
                        Right outer join is the complement of left outer join. All rows from the
                        right operand table are included, matching rows from the left operand table
                        included, and the columns from the left operand table are filled with NULLs
                        if no row exists matching the right table.
                    </p>
<p>
                        Some RDBMS brands don't support this join type, but in general any right
                        join can be represented as a left join by reversing the order of the
                        tables.
                    </p>
</li>
<li>
<p>
                        <span><strong class="command">FULL JOIN</strong></span> with the
                        <code class="code">joinFull(table, condition, [columns])</code> method.
                    </p>
<p>
                        A full outer join is like combining a left outer join and a right outer
                        join. All rows from both tables are included, paired with each other on the
                        same row of the result set if they satisfy the join condition, and
                        otherwise paired with NULLs in place of columns from the other table.
                    </p>
<p>
                        Some RDBMS brands don't support this join type.
                    </p>
</li>
<li>
<p>
                        <span><strong class="command">CROSS JOIN</strong></span> with the
                        <code class="code">joinCross(table, [columns])</code> method.
                    </p>
<p>
                        A cross join is a Cartesian product. Every row in the first table is
                        matched to every row in the second table. Therefore the number of rows in
                        the result set is equal to the product of the number of rows in each table.
                        You can filter the result set using conditions in a WHERE clause; in this
                        way a cross join is similar to the old SQL-89 join syntax.
                    </p>
<p>
                        The <code class="code">joinCross()</code> method has no parameter to specify the join
                        condition. Some RDBMS brands don't support this join type.
                    </p>
</li>
<li>
<p>
                        <span><strong class="command">NATURAL JOIN</strong></span> with the
                        <code class="code">joinNatural(table, [columns])</code> method.
                    </p>
<p>
                        A natural join compares any column(s) that appear with the same name in
                        both tables. The comparison is equality of all the column(s); comparing the
                        columns using inequality is not a natural join. Only natural inner joins
                        are supported by this API, even though SQL permits natural outer joins as
                        well.
                    </p>
<p>
                        The <code class="code">joinNatural()</code> method has no parameter to specify the join
                        condition.
                    </p>
</li>
</ul></div>
<p>
                In addition to these join methods, you can simplify your queries by using the
                JoinUsing methods. Instead of supplying a full condition to your join, you simply
                pass the column name on which to join and the Zend_Db_Select object completes the
                condition for you.
            </p>
<div class="example">
<a name="zend.db.select.building.joinusing.example"></a><p class="title"><b>Example 10.54. Example of the joinUsing() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT *
//   FROM "table1"
//   JOIN "table2"
//   ON "table1".column1 = "table2".column1
//   WHERE column2 = 'foo'

$select = $db-&gt;select()
    -&gt;from('table1')
    -&gt;joinUsing('table2', 'column1')
    -&gt;where('column2 = ?', 'foo');
                </pre></div>
</div>
<br class="example-break"><p>
                Each of the applicable join methods in the Zend_Db_Select component has a
                corresponding 'using' method.
            </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                        <code class="code">joinUsing(table, join, [columns])</code> and
                        <code class="code">joinInnerUsing(table, join, [columns])</code>
                    </p></li>
<li><p>
                        <code class="code">joinLeftUsing(table, join, [columns])</code>
                    </p></li>
<li><p>
                        <code class="code">joinRightUsing(table, join, [columns])</code>
                    </p></li>
<li><p>
                        <code class="code">joinFullUsing(table, join, [columns])</code>
                    </p></li>
</ul></div>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.where"></a>10.4.3.6. Adding a WHERE Clause</h4></div></div></div>
<p>
                You can specify criteria for restricting rows of the result set using the
                <code class="code">where()</code> method. The first argument of this method is a SQL expression,
                and this expression is used in a SQL <code class="code">WHERE</code> clause in the query.
            </p>
<div class="example">
<a name="zend.db.select.building.where.example"></a><p class="title"><b>Example 10.55. Example of the where() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE price &gt; 100.00

$select = $db-&gt;select()
    -&gt;from(
        'products',
        array('product_id', 'product_name', 'price'))
    -&gt;where('price &gt; 100.00');
                </pre></div>
</div>
<br class="example-break"><div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    No quoting is applied to expressions given to the <code class="code">where()</code> or
                    <code class="code">orWhere()</code> methods. If you have column names that need to be
                    quoted, you must use <code class="code">quoteIdentifier()</code> as you form the string for
                    the condition.
                </p></td></tr>
</table></div>
<p>
                The second argument to the <code class="code">where()</code> method is optional. It is a value
                to substitute into the expression. Zend_Db_Select quotes the value and substitutes
                it for a question-mark ("<code class="code">?</code>") symbol in the expression.
            </p>
<p>
                This method accepts only one parameter. If you have an expression into which you
                need to substitute multiple variables, you must format the string manually,
                interpolating variables and performing quoting yourself.
            </p>
<div class="example">
<a name="zend.db.select.building.where.example-param"></a><p class="title"><b>Example 10.56. Example of a parameter in the where() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (price &gt; 100.00)

$minimumPrice = 100;

$select = $db-&gt;select()
    -&gt;from(
        'products',
        array('product_id', 'product_name', 'price'))
    -&gt;where('price &gt; ?', $minimumPrice);
                </pre></div>
</div>
<br class="example-break"><p>
                You can invoke the <code class="code">where()</code> method multiple times on the same
                Zend_Db_Select object. The resulting query combines the multiple terms together
                using <code class="code">AND</code> between them.
            </p>
<div class="example">
<a name="zend.db.select.building.where.example-and"></a><p class="title"><b>Example 10.57. Example of multiple where() methods</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (price &gt; 100.00)
//     AND (price &lt; 500.00)

$minimumPrice = 100;
$maximumPrice = 500;

$select = $db-&gt;select()
    -&gt;from('products',
        array('product_id', 'product_name', 'price'))
    -&gt;where('price &gt; ?', $minimumPrice)
    -&gt;where('price &lt; ?', $maximumPrice);
                </pre></div>
</div>
<br class="example-break"><p>
                If you need to combine terms together using <code class="code">OR</code>, use the
                <code class="code">orWhere()</code> method. This method is used in the same way as the
                <code class="code">where()</code> method, except that the term specified is preceded by
                <code class="code">OR</code>, instead of <code class="code">AND</code>.
            </p>
<div class="example">
<a name="zend.db.select.building.where.example-or"></a><p class="title"><b>Example 10.58. Example of the orWhere() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (price &lt; 100.00)
//     OR (price &gt; 500.00)

$minimumPrice = 100;
$maximumPrice = 500;

$select = $db-&gt;select()
    -&gt;from('products',
        array('product_id', 'product_name', 'price'))
    -&gt;where('price &lt; ?', $minimumPrice)
    -&gt;orWhere('price &gt; ?', $maximumPrice);
                </pre></div>
</div>
<br class="example-break"><p>
                Zend_Db_Select automatically puts parentheses around each expression you specify
                using the <code class="code">where()</code> or <code class="code">orWhere()</code> methods. This helps to
                ensure that Boolean operator precedence does not cause unexpected results.
            </p>
<div class="example">
<a name="zend.db.select.building.where.example-parens"></a><p class="title"><b>Example 10.59. Example of parenthesizing Boolean expressions</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT product_id, product_name, price
//   FROM "products"
//   WHERE (price &lt; 100.00 OR price &gt; 500.00)
//     AND (product_name = 'Apple')

$minimumPrice = 100;
$maximumPrice = 500;
$prod = 'Apple';

$select = $db-&gt;select()
    -&gt;from('products',
        array('product_id', 'product_name', 'price'))
    -&gt;where("price &lt; $minimumPrice OR price &gt; $maximumPrice")
    -&gt;where('product_name = ?', $prod);
                </pre></div>
</div>
<br class="example-break"><p>
                In the example above, the results would be quite different without the parentheses,
                because <code class="code">AND</code> has higher precedence than <code class="code">OR</code>. Zend_Db_Select
                applies the parentheses so the effect is that each expression in successive calls
                to the <code class="code">where()</code> bind more tightly than the <code class="code">AND</code> that
                combines the expressions.
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.group"></a>10.4.3.7. Adding a GROUP BY Clause</h4></div></div></div>
<p>
                In SQL, the <code class="code">GROUP BY</code> clause allows you to reduce the rows of a query
                result set to one row per unique value found in the column(s) named in the
                <code class="code">GROUP BY</code> clause.
            </p>
<p>
                In Zend_Db_Select, you can specify the column(s) to use for calculating the groups
                of rows using the <code class="code">group()</code> method. The argument to this method is a
                column or an array of columns to use in the <code class="code">GROUP BY</code> clause.
            </p>
<div class="example">
<a name="zend.db.select.building.group.example"></a><p class="title"><b>Example 10.60. Example of the group() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", COUNT(*) AS line_items_per_product
//   FROM "products" AS p JOIN "line_items" AS l
//     ON p.product_id = l.product_id
//   GROUP BY p.product_id

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id'))
    -&gt;join(array('l' =&gt; 'line_items'),
        'p.product_id = l.product_id',
        array('line_items_per_product' =&gt; 'COUNT(*)'))
    -&gt;group('p.product_id');
                </pre></div>
</div>
<br class="example-break"><p>
                Like the columns array in the <code class="code">from()</code> method, you can use correlation
                names in the column name strings, and the column is quoted as an identifier unless
                the string contains parentheses or is an object of type Zend_Db_Expr.
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.having"></a>10.4.3.8. Adding a HAVING Clause</h4></div></div></div>
<p>
                In SQL, the <code class="code">HAVING</code> clause applies a restriction condition on groups of
                rows. This is similar to how a <code class="code">WHERE</code> clause applies a restriction
                condition on rows. But the two clauses are different because <code class="code">WHERE</code>
                conditions are applied before groups are defined, whereas <code class="code">HAVING</code>
                conditions are applied after groups are defined.
            </p>
<p>
                In Zend_Db_Select, you can specify conditions for restricting groups using the
                <code class="code">having()</code> method. Its usage is similar to that of the
                <code class="code">where()</code> method. The first argument is a string containing a SQL
                expression. The optional second argument is a value that is used to replace a
                positional parameter placeholder in the SQL expression. Expressions given in
                multiple invocations of the <code class="code">having()</code> method are combined using the
                Boolean <code class="code">AND</code> operator, or the <code class="code">OR</code> operator if you use the
                <code class="code">orHaving()</code> method.
            </p>
<div class="example">
<a name="zend.db.select.building.having.example"></a><p class="title"><b>Example 10.61. Example of the having() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", COUNT(*) AS line_items_per_product
//   FROM "products" AS p JOIN "line_items" AS l
//     ON p.product_id = l.product_id
//   GROUP BY p.product_id
//   HAVING line_items_per_product &gt; 10

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id'))
    -&gt;join(array('l' =&gt; 'line_items'),
        'p.product_id = l.product_id',
        array('line_items_per_product' =&gt; 'COUNT(*)'))
    -&gt;group('p.product_id')
    -&gt;having('line_items_per_product &gt; 10');
                </pre></div>
</div>
<br class="example-break"><div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    No quoting is applied to expressions given to the <code class="code">having()</code> or
                    <code class="code">orHaving()</code> methods. If you have column names that need to be
                    quoted, you must use <code class="code">quoteIdentifier()</code> as you form the string for
                    the condition.
                </p></td></tr>
</table></div>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.order"></a>10.4.3.9. Adding an ORDER BY Clause</h4></div></div></div>
<p>
                In SQL, the <code class="code">ORDER BY</code> clause specifies one or more columns or
                expressions by which the result set of a query is sorted. If multiple columns are
                listed, the secondary columns are used to resolve ties; the sort order is
                determined by the secondary columns if the preceding columns contain identical
                values. The default sorting is from least value to greatest value. You can also
                sort by greatest value to least value for a given column in the list by specifying
                the keyword <code class="code">DESC</code> after that column.
            </p>
<p>
                In Zend_Db_Select, you can use the <code class="code">order()</code> method to specify a column
                or an array of columns by which to sort. Each element of the array is a string
                naming a column. Optionally with the <code class="code">ASC</code> <code class="code">DESC</code> keyword
                following it, separated by a space.
            </p>
<p>
                Like in the <code class="code">from()</code> and <code class="code">group()</code> methods, column names are
                quoted as identifiers, unless they contain contain parentheses or are an object of
                type Zend_Db_Expr.
            </p>
<div class="example">
<a name="zend.db.select.building.order.example"></a><p class="title"><b>Example 10.62. Example of the order() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", COUNT(*) AS line_items_per_product
//   FROM "products" AS p JOIN "line_items" AS l
//     ON p.product_id = l.product_id
//   GROUP BY p.product_id
//   ORDER BY "line_items_per_product" DESC, "product_id"

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'),
        array('product_id'))
    -&gt;join(array('l' =&gt; 'line_items'),
        'p.product_id = l.product_id',
        array('line_items_per_product' =&gt; 'COUNT(*)'))
    -&gt;group('p.product_id')
    -&gt;order(array('line_items_per_product DESC', 'product_id'));
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.limit"></a>10.4.3.10. Adding a LIMIT Clause</h4></div></div></div>
<p>
                Some RDBMS brands extend SQL with a query clause known as the <code class="code">LIMIT</code>
                clause. This clause reduces the number of rows in the result set to at most a
                number you specify. You can also specify to skip a number of rows before starting
                to output. This feature makes it easy to take a subset of a result set, for example
                when displaying query results on progressive pages of output.
            </p>
<p>
                In Zend_Db_Select, you can use the <code class="code">limit()</code> method to specify the count
                of rows and the number of rows to skip. The first argument to this method is the
                desired count of rows. The second argument is the number of rows to skip.
            </p>
<div class="example">
<a name="zend.db.select.building.limit.example"></a><p class="title"><b>Example 10.63. Example of the limit() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p
//   LIMIT 10, 20

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'), array('product_id', 'product_name'))
    -&gt;limit(10, 20);
                </pre></div>
</div>
<br class="example-break"><div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    The <code class="code">LIMIT</code> syntax is not supported by all RDBMS brands. Some RDBMS
                    require different syntax to support similar functionality. Each
                    Zend_Db_Adapter_Abstract class includes a method to produce SQL appropriate for
                    that RDBMS.
                </p></td></tr>
</table></div>
<p>
                Use the <code class="code">limitPage()</code> method for an alternative way to specify row count
                and offset. This method allows you to limit the result set to one of a series of
                fixed-length subsets of rows from the query's total result set. In other words, you
                specify the length of a "page" of results, and the ordinal number of the single
                page of results you want the query to return. The page number is the first argument
                of the <code class="code">limitPage()</code> method, and the page length is the second argument.
                Both arguments are required; they have no default values.
            </p>
<div class="example">
<a name="zend.db.select.building.limit.example2"></a><p class="title"><b>Example 10.64. Example of the limitPage() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p."product_id", p."product_name"
//   FROM "products" AS p
//   LIMIT 10, 20

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products'), array('product_id', 'product_name'))
    -&gt;limitPage(2, 10);
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.distinct"></a>10.4.3.11. Adding the DISTINCT Query Modifier</h4></div></div></div>
<p>
                The <code class="code">distinct()</code> method enables you to add the <code class="code">DISTINCT</code>
                keyword to your SQL query.
            </p>
<div class="example">
<a name="zend.db.select.building.distinct.example"></a><p class="title"><b>Example 10.65. Example of the distinct() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT DISTINCT p."product_name"
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;distinct()
    -&gt;from(array('p' =&gt; 'products'), 'product_name');
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.building.for-update"></a>10.4.3.12. Adding the FOR UPDATE Query Modifier</h4></div></div></div>
<p>
                The <code class="code">forUpdate()</code> method enables you to add the <code class="code">FOR UPDATE</code>
                modifier to your SQL query.
            </p>
<div class="example">
<a name="zend.db.select.building.for-update.example"></a><p class="title"><b>Example 10.66. Example of forUpdate() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT FOR UPDATE p.*
//   FROM "products" AS p

$select = $db-&gt;select()
    -&gt;forUpdate()
    -&gt;from(array('p' =&gt; 'products'));
                </pre></div>
</div>
<br class="example-break">
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.db.select.execute"></a>10.4.4. Executing Select Queries</h3></div></div></div>
<p>
            This section describes how to execute the query represented by a Zend_Db_Select object.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.execute.query-adapter"></a>10.4.4.1. Executing Select Queries from the Db Adapter</h4></div></div></div>
<p>
                You can execute the query represented by the Zend_Db_Select object by passing it as
                the first argument to the <code class="code">query()</code> method of a Zend_Db_Adapter_Abstract
                object. Use the Zend_Db_Select objects instead of a string query.
            </p>
<p>
                The <code class="code">query()</code> method returns an object of type Zend_Db_Statement or
                PDOStatement, depending on the adapter type.
            </p>
<div class="example">
<a name="zend.db.select.execute.query-adapter.example"></a><p class="title"><b>Example 10.67. Example using the Db adapter's query() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$select = $db-&gt;select()
  -&gt;from('products');

$stmt = $db-&gt;query($select);
$result = $stmt-&gt;fetchAll();
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.execute.query-select"></a>10.4.4.2. Executing Select Queries from the Object</h4></div></div></div>
<p>
                As an alternative to using the <code class="code">query()</code> method of the adapter object,
                you can use the <code class="code">query()</code> method of the Zend_Db_Select object. Both
                methods return an object of type Zend_Db_Statement or PDOStatement, depending on
                the adapter type.
            </p>
<div class="example">
<a name="zend.db.select.execute.query-select.example"></a><p class="title"><b>Example 10.68. Example using the Select object's query method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$select = $db-&gt;select()
    -&gt;from('products');

$stmt = $select-&gt;query();
$result = $stmt-&gt;fetchAll();
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.execute.tostring"></a>10.4.4.3. Converting a Select Object to a SQL String</h4></div></div></div>
<p>
                If you need access to a string representation of the SQL query corresponding to the
                Zend_Db_Select object, use the <code class="code">__toString()</code> method.
            </p>
<div class="example">
<a name="zend.db.select.execute.tostring.example"></a><p class="title"><b>Example 10.69. Example of the __toString() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$select = $db-&gt;select()
    -&gt;from('products');

$sql = $select-&gt;__toString();
echo "$sql\n";

// The output is the string:
//   SELECT * FROM "products"
                </pre></div>
</div>
<br class="example-break">
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.db.select.other"></a>10.4.5. Other methods</h3></div></div></div>
<p>
            This section describes other methods of the Zend_Db_Select class that are not covered
            above: <code class="code">getPart()</code> and <code class="code">reset()</code>.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.other.get-part"></a>10.4.5.1. Retrieving Parts of the Select Object</h4></div></div></div>
<p>
                The <code class="code">getPart()</code> method returns a representation of one part of your SQL
                query. For example, you can use this method to return the array of expressions for
                the <code class="code">WHERE</code> clause, or the array of columns (or column expressions) that
                are in the <code class="code">SELECT</code> list, or the values of the count and offset for the
                <code class="code">LIMIT</code> clause.
            </p>
<p>
                The return value is not a string containing a fragment of SQL syntax. The return
                value is an internal representation, which is typically an array structure
                containing values and expressions. Each part of the query has a different
                structure.
            </p>
<p>
                The single argument to the <code class="code">getPart()</code> method is a string that
                identifies which part of the Select query to return. For example, the string
                <code class="code">'from'</code> identifies the part of the Select object that stores
                information about the tables in the <code class="code">FROM</code> clause, including joined
                tables.
            </p>
<p>
                The Zend_Db_Select class defines constants you can use for parts of the SQL query.
                You can use these constant definitions, or you can the literal strings.
            </p>
<div class="table">
<a name="zend.db.select.other.get-part.table"></a><p class="title"><b>Table 10.2. Constants used by getPart() and reset()</b></p>
<div class="table-contents"><table summary="Constants used by getPart() and reset()" border="1">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>Constant</th>
<th>String value</th>
</tr></thead>
<tbody>
<tr>
<td><code class="code">Zend_Db_Select::DISTINCT</code></td>
<td><code class="code">'distinct'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::FOR_UPDATE</code></td>
<td><code class="code">'forupdate'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::COLUMNS</code></td>
<td><code class="code">'columns'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::FROM</code></td>
<td><code class="code">'from'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::WHERE</code></td>
<td><code class="code">'where'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::GROUP</code></td>
<td><code class="code">'group'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::HAVING</code></td>
<td><code class="code">'having'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::ORDER</code></td>
<td><code class="code">'order'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::LIMIT_COUNT</code></td>
<td><code class="code">'limitcount'</code></td>
</tr>
<tr>
<td><code class="code">Zend_Db_Select::LIMIT_OFFSET</code></td>
<td><code class="code">'limitoffset'</code></td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="example">
<a name="zend.db.select.other.get-part.example"></a><p class="title"><b>Example 10.70. Example of the getPart() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
$select = $db-&gt;select()
    -&gt;from('products')
    -&gt;order('product_id');

// You can use a string literal to specify the part
$orderData = $select-&gt;getPart( 'order' );

// You can use a constant to specify the same part
$orderData = $select-&gt;getPart( Zend_Db_Select::ORDER );

// The return value may be an array structure, not a string.
// Each part has a different structure.
print_r( $orderData );
                </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.db.select.other.reset"></a>10.4.5.2. Resetting Parts of the Select Object</h4></div></div></div>
<p>
                The <code class="code">reset()</code> method enables you to clear one specified part of the SQL
                query, or else clear all parts of the SQL query if you omit the argument.
            </p>
<p>
                The single argument is optional. You can specify the part of the query to clear,
                using the same strings you used in the argument to the <code class="code">getPart()</code>
                method. The part of the query you specify is reset to a default state.
            </p>
<p>
                If you omit the parameter, <code class="code">reset()</code> changes all parts of the query to
                their default state. This makes the Zend_Db_Select object equivalent to a new
                object, as though you had just instantiated it.
            </p>
<div class="example">
<a name="zend.db.select.other.reset.example"></a><p class="title"><b>Example 10.71. Example of the reset() method</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
// Build this query:
//   SELECT p.*
//   FROM "products" AS p
//   ORDER BY "product_name"

$select = $db-&gt;select()
    -&gt;from(array('p' =&gt; 'products')
    -&gt;order('product_name');

// Changed requirement, instead order by a different columns:
//   SELECT p.*
//   FROM "products" AS p
//   ORDER BY "product_id"

// Clear one part so we can redefine it
$select-&gt;reset( Zend_Db_Select::ORDER );

// And specify a different column
$select-&gt;order('product_id');

// Clear all parts of the query
$select-&gt;reset();
                </pre></div>
</div>
<br class="example-break">
</div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.db.profiler.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.db.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.db.table.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">10.3. Zend_Db_Profiler </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 10.5. Zend_Db_Table</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
